﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DrugStore.Objects;
using DrugStore.Library;
using System.Data.SqlClient;
using System.Configuration;
using BLToolkit.Data;


namespace DrugStore.FormCollect
{
    public partial class QuanLyNguoiSuDung : Form
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static List<NguoiSuDung> ListUser;
        private int currentID;
        private NguoiSuDung us = new NguoiSuDung();
        private static DbManager db = new DbManager();
             
        public QuanLyNguoiSuDung()
        {
            InitializeComponent();
        }

        private void bt_addUser_Click(object sender, EventArgs e)
        {
            QuanLyNguoiSuDung_AddNew fr = new QuanLyNguoiSuDung_AddNew(this);
            fr.Show();
        }

        private void tb_NoiCapCMND_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_userName_TextChanged(object sender, EventArgs e)
        {
            lb_ThongBao.Text = "";
        }

        private void tb_password_TextChanged(object sender, EventArgs e)
        {
            lb_ThongBao.Text = "";
        }

        private void tb_userName_Leave(object sender, EventArgs e)
        {
            if (DBFactory.GetListItem<NguoiSuDung>(db).Where(user => user.UserName == tb_userName.Text).ToList().Count > 0)
                lb_ThongBao.Text = "Tên đăng nhập này đã có người sử dụng, vui lòng chọn tên đăng nhập khác";
            this.Text = "";
            this.Focus();
        }

        private void QuanLyNguoiSuDung_Load(object sender, EventArgs e)
        {
            var source = new BindingSource();
            ListUser = DBFactory.GetListItem<NguoiSuDung>(db).ToList();
            source.DataSource = ListUser.Select(a => new {a.ID, a.UserName, a.HoTen }).ToList();
            dataGridView1.DataSource = source;
            /*
            //this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["UserName"].HeaderText = "Tên đăng nhập";
            this.dataGridView1.Columns["UserName"].Width =150;
            this.dataGridView1.Columns["HoTen"].HeaderText = "Tên người sử dụng";
            this.dataGridView1.Columns["HoTen"].Width = 180;
             */
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
                return;
            string ta = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            int id;
            if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(), out id))
            {
                currentID = id;
                NguoiSuDung su = ListUser.Where(f => f.ID == id).First();
                us = su;
                tb_CMND.Text = su.CMND;
                tb_diachi.Text = su.DiaChi;
                tb_Email1.Text = su.Email_1;
                tb_Email2.Text = su.Email_2;
                tb_FullName.Text = su.HoTen;
                tb_major.Text = su.TrinhDo;
                tb_Task.Text = su.MoTaCongViec;
                tb_userName.Text = su.UserName;
                tb_password.Text = su.PassWord;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0 && currentID != null)
            {
                if (DBFactory.DeleteItem<NguoiSuDung>(us, db))
                {
                    MessageBox.Show("Xóa người sử dụng thành công");
                    ClearControl();
                    this.QuanLyNguoiSuDung_Load(sender, e);
                }
            }
        }

        private void ClearControl()
        {
            panel2.Controls.ClearControls();
            groupBox1.Controls.ClearControls();
            groupBox2.Controls.ClearControls();
        }

        public event EventHandler ButtonClicked;

        public void NotifyButtonClicked(EventArgs e)
        {
            if (ButtonClicked != null)
                ButtonClicked(this, e);

        }

        private void QuanLyNguoiSuDung_Activated(object sender, EventArgs e)
        {
            QuanLyNguoiSuDung_Load(sender, e);
        }

        private void QuanLyNguoiSuDung_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainFormAdmin main = new MainFormAdmin();
            main.DestroyChild(this.Name);
        }

        public void ReloadForm()
        {
            var source = new BindingSource();
            ListUser = DBFactory.GetListItem<NguoiSuDung>(db).ToList();
            source.DataSource = ListUser.Select(a => new { a.ID, a.UserName, a.HoTen }).ToList();
            dataGridView1.DataSource = source;
            /*
            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["UserName"].HeaderText = "Tên đăng nhập";
            this.dataGridView1.Columns["UserName"].Width = 150;
            this.dataGridView1.Columns["HoTen"].HeaderText = "Tên người sử dụng";
            this.dataGridView1.Columns["HoTen"].Width = 180;
             */
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                us.CMND = tb_CMND.Text;
                us.DiaChi = tb_diachi.Text;
                us.DienThoai_1 = tb_Email1.Text;
                us.DienThoai_2 = tb_Phone1.Text;
                us.Email_1 = tb_Email1.Text;
                us.Email_2 = tb_Email2.Text;
                us.HoTen = tb_FullName.Text;
                //us.MaCongViec = tb_major.Text;
                us.NgaySinh = dt_DoB.Value;
                us.TrinhDo = tb_major.Text;

                DBFactory.Update<NguoiSuDung>(us, db);
                lb_ThongBao.Text = ("Cập nhật thông tin thành công");
            }
            catch (Exception ex)
            {
                throw ex;
            }

            
        }
    }
}